package com.pqc.tbk.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pqc.tbk.service.admin.SystemService;

@Controller
@RequestMapping("/admin/config")
public class SystemController {

	private SystemService systemService;

	@Autowired
	public void setSystemService(final SystemService systemService) {
		this.systemService = systemService;
	}

	@RequestMapping(value = { "", "show" }, method = RequestMethod.GET)
	public String show(final Model model) {

		model.addAttribute("settings", systemService.convertToMap());
		return "admin/config/show";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String config(final String system_website, final String system_name,
			final String system_icp, final String email_authuser,
			final String email_authpassword, final String email_smtphost,
			final String email_smtpport, final String taobao_appkey,
			final String taobao_appsecret, final String taobao_session,
			final String taobao_submiturl) {
		System.out.println(email_authuser);
		String res = "{'state', 'ok'}";
		try {
			systemService.updateProperties(system_website, system_name,
					system_icp, email_authuser, email_authpassword,
					email_smtphost, email_smtpport, taobao_appkey,
					taobao_appsecret, taobao_session, taobao_submiturl);
		} catch (final Exception e) {
			res = "{'state', 'error'}";
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * jsp探针
	 * 
	 * @return
	 */
	@RequestMapping("probe")
	public String probe() {
		return "admin/config/probe";
	}
}
